package com.ruoyi.framework.aspectj;

import com.ruoyi.common.annotation.PrintLnTime;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.PrintUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

@Aspect
@Component
public class TimeAspect {

    @Pointcut("execution(public * com.ruoyi.erp.controller.*.*(..))")
    public void ResultAspect(){
    }

    @Around("@annotation(printLnTime)")
    public Object printLnTimeAround(ProceedingJoinPoint joinPoint, PrintLnTime printLnTime) throws Throwable {
        //接收到请求，记录请求内容
        Signature signature = joinPoint.getSignature();
        String className = joinPoint.getTarget().getClass().getSimpleName();
        Method method = ((MethodSignature) signature).getMethod();
        String methodName = method.getName();
        // 设置请求方式
        String requestMethod = ServletUtils.getRequest().getMethod();

        String printImprotMethod = "==================  进入  " + className + ":方法:" + methodName + "():請求方式:"+ requestMethod +"  =====================================";
        PrintUtil.BLUE.Println(printImprotMethod);

        // 获取当前的用户
        LoginUser currentUser = SecurityUtils.getLoginUser();
        String userName = currentUser.getUsername();
        String printImprotMethod2 = "==================  用户  ==================  " + userName + "  ==================";
        PrintUtil.CYAN.Println(printImprotMethod2);
        //执行方法
        long startTime = System.currentTimeMillis();   //获取开始时间
        //注意，如果调用joinPoint.proceed()方法，则修改的参数值不会生效，必须调用joinPoint.proceed(Object[] args)
        Object proceed = joinPoint.proceed();
        //方法执行完成后台操作
        long endTime = System.currentTimeMillis(); //获取结束时间
        long interval = endTime - startTime;//运行的时间 毫秒
        SimpleDateFormat formatter = new SimpleDateFormat("HH");//初始化Formatter的转换格式。
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String hour = formatter.format(interval);
        formatter.applyPattern("mm");
        String minute = formatter.format(interval);
        formatter.applyPattern("ss");
        String second = formatter.format(interval);
        PrintUtil.RED.Println(className + "类的方法:" + methodName + ",执行时间： " + hour + "小时,"
                + minute + "分钟,"
                + second + "秒,共" + interval + "毫秒"
        );
        PrintUtil.BLUE.Println("==================  方法执行完毕  ===================================================================");
        System.out.println();

        //如果这里不返回result，则目标对象实际返回值会被置为null
        return proceed;
    }

//    @AfterReturning(pointcut = "ResultAspect()", returning = "returnObject")
//    public void resultAfterReturning(JoinPoint joinPoint, Object returnObject) {
//        resultAfter(returnObject);
//    }
//
//    @AfterReturning(pointcut = "ResultExceptionHandlerAspect()", returning = "returnObject")
//    public void resultAfterExceptionHandlerReturning(JoinPoint joinPoint, Object returnObject) {
//        resultAfter(returnObject);
//    }
}
